package gold.digger;

import gold.utils.InputUtil;

import java.util.*;

/**
 * Created by fanzhenyu02 on 2021/12/10.
 * common problem solver template.
 */
public class LC983 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {

        Map<Integer, Integer> memo = new HashMap<>();

        public int mincostTickets(int[] days, int[] costs) {
            return dfs(days, costs, 0);
        }

        public int dfs(int[] days, int[] costs, int pos) {
            if (pos >= days.length) return 0;
            if (memo.containsKey(pos)) return memo.get(pos);

            // 当前pos选1日票
            int oneDayTicketCost = dfs(days, costs, findNextDay(days, pos, 1)) + costs[0];
            int sevenDayTicketCost = dfs(days, costs, findNextDay(days, pos, 7)) + costs[1];
            int thirtyTicketCost = dfs(days, costs, findNextDay(days, pos, 30)) + costs[2];
            int curAns = Math.min(Math.min(oneDayTicketCost, sevenDayTicketCost), thirtyTicketCost);
            memo.put(pos, curAns);
            return curAns;
        }

        public int findNextDay(int[] days, int pos, int interval) {
            int nextIdx = pos + 1;
            while (nextIdx < days.length && days[nextIdx] < days[pos] + interval) nextIdx++;
            return nextIdx;
        }
    }

    public void run() {
        Solution solution = new Solution();
//        System.out.println(solution.mincostTickets(InputUtil.toIntegerArray("[1,4,6,7,8,20]"), InputUtil.toIntegerArray("[2,7,15]")));
        System.out.println(solution.mincostTickets(InputUtil.toIntegerArray("[1,2,3,4,5,6,7,8,9,10,30,31]"), InputUtil.toIntegerArray("[2,7,15]")));
    }

    public static void main(String[] args) throws Exception {
        LC983 an = new LC983();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
